﻿using System;
using System.Data;
using Epm.AppLogic.Framework;
//using System.Runtime.Caching;

namespace msBsc.SvRef
{ 

    public  class ApplicationConst : IDisposable
    {
        private static ApplicationConst _Const = new ApplicationConst();
        public static ApplicationConst Const
        {
            get
            {
                if (_Const == null)
                {
                    lock (_Const)
                    {
                        _Const = new ApplicationConst();
                    }
                }
                return _Const;
            }
        }
 
        public ApplicationConst()
        {
            //initdicSeekData();
            //initdicDataConst();
            //DataSet ds= dsSysUser;
        }

        
        //在.NET的对象中实际上有两个用于释放资源的函数：Dispose和Finalize。Finalize的目的是用于释放非托管的资源，而Dispose是用于释放所有资源，包括托管的和非托管的。
        
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    // Release managed resources
                    _dicSeekData.Dispose();
                    //_dicDataConst.Dispose();
                }

                // Release unmanaged resources

                m_disposed = true;
            }
        }

        ~ApplicationConst()
        {
            Dispose(false);
        }

        private bool m_disposed;

         

        public bool IsWebSys = true;



        /// <summary>
        /// 店铺ID
        /// </summary>
        //public string OperDealerId
        //{
        //    get
        //    {
        //        return UserInfo.OperDealerId;
        //    }
        //    set
        //    {
        //        UserInfo.OperDealerId = value;
        //    }
        //}




        /// <summary>
        /// 从内存获取当前配置
        /// </summary>
        /// <param name="strSettingsNme"></param>
        /// <returns></returns>
        public  string GetAppSettings(string strSettingsNme)
        {
            return msBsc.AppLogic.Cfg.SystemSet.oSystemSet.GetAppSettings(strSettingsNme);
        }


        private msBsc.AppLogic.Cfg.SynDictionary _dicSeekData = new msBsc.AppLogic.Cfg.SynDictionary(true);
        /// <summary>
        /// VALUE_LIST_EXEC_SQL,VALUE_LIST,VALUE_LIST_FIELD
        /// </summary>
        public msBsc.AppLogic.Cfg.SynDictionary dicSeekData
        {
            get
            {
                if (_dicSeekData.Count <= 0)
                {
                    initdicSeekData();
                }
                return _dicSeekData;
            }
        }
        public void initdicSeekData()
        {
            using (CDbAccess objtem1 = new CDbAccess())
            {
                DataSet _ds = objtem1.QueryBySQL("SELECT SEEK_NO,VALUE_LIST_EXEC_SQL,VALUE_LIST,VALUE_LIST_FIELD FROM BSE_DATA_SEEK_SET WHERE REC_ACTIVE=1");
                _dicSeekData.Clear();
                for (int i = _ds.Tables[0].Rows.Count - 1; i >= 0; i--)
                {
                    _dicSeekData.Add(_ds.Tables[0].Rows[i]["SEEK_NO"].ToString(),new string[]{
                    _ds.Tables[0].Rows[i]["VALUE_LIST_EXEC_SQL"].ToString()
                    ,_ds.Tables[0].Rows[i]["VALUE_LIST"].ToString()
                    ,_ds.Tables[0].Rows[i]["VALUE_LIST_FIELD"].ToString()
                    });
                }
            }
        }

        public msBsc.AppLogic.Cfg.SynDictionary dicAppData = new msBsc.AppLogic.Cfg.SynDictionary(true);
         
          

        public string GetNewSerialNo(string strUSAGE_CID, string strPrefix, int iSerialLength)
        {
            using (CDbAccess objtem1 = new CDbAccess())
            {
                try
                {
                    if (string.IsNullOrEmpty(strUSAGE_CID))
                        return string.Empty;

                    DataSet ds = objtem1.QueryBySQL("SELECT * FROM SYS_SERIAL_NUMBER where USAGE_CID = '" + strUSAGE_CID + "' and PREFIX = '" + strPrefix + "'");

                    int iLastSerialNum = 0;
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        objtem1.ExecuteBySQL("insert SYS_SERIAL_NUMBER values(newid(),'" + strUSAGE_CID + "','" + strPrefix + "',6,1,999999,1,1)");
                        iLastSerialNum = 0;
                    }
                    else
                    {
                        iLastSerialNum = ds.Tables[0].Rows[0]["LAST_SERIAL_NUMBER"].ToString() == string.Empty ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["LAST_SERIAL_NUMBER"]);
                    }

                    if (iSerialLength == 0)
                        iSerialLength = (ds.Tables[0].Rows[0]["SERIAL_LENGTH"].ToString() == string.Empty || ds.Tables[0].Rows[0]["SERIAL_LENGTH"].ToString() == "0") ? 6 : Convert.ToInt32(ds.Tables[0].Rows[0]["SERIAL_LENGTH"]);
                    iLastSerialNum++;
                    objtem1.ExecuteBySQL("UPDATE SYS_SERIAL_NUMBER SET LAST_SERIAL_NUMBER=" + iLastSerialNum.ToString() + " where USAGE_CID = '" + strUSAGE_CID + "' and PREFIX = '" + strPrefix + "'");

                    return strPrefix + iLastSerialNum.ToString().PadLeft(iSerialLength, '0');


                }
                catch (Exception e)
                {
                    throw e;
                }
            } 
        }

        /// <summary>
        /// 获取唯一标识
        /// </summary>
        /// <returns></returns>
        public long GeneratelongId()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(buffer, 0);
        }

        /// <summary>
        /// 获取唯一标识
        /// </summary>
        /// <returns></returns>
        public string GenerateStrId()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= ((int)b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }
        /// <summary>
        /// 获取唯一标识，CDbAccess
        /// </summary>
        /// <returns></returns>
        public string GetId()
        {
            //Epm.AppLogic.Framework.CDbAccess oDataObj = new Epm.AppLogic.Framework.CDbAccess();
            //oDataObj.IdGroup = "RID";
            using (CDbAccess objtem1 = new CDbAccess())
            {
                return objtem1.NewID();
            }
        }
             
    }
}
